package com.example.mytestdemo.customviewbook.two

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.example.mytestdemo.R

/**
 * @author tgw
 * @date 2023/2/8
 * @describe 折叠的view
 */
class PolytoPolyViewView4 @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {

    var bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.cat)

    var sTransHeight = 50f
    var mFoldWidth = 0f
    var spiltNum = 8
     var mMatrix:Array<Matrix> =Array(spiltNum){
        Matrix()
    }
    //j假设折叠后变为原来的0.8倍
    var mFactory = 0.8f

    var mSolidPaint = Paint()
    var mShadowPaint = Paint()
    lateinit var mLinearGradient : LinearGradient


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        //初始化阴影颜色
        var alpha = (255*(1-0.8)).toInt()
        mSolidPaint.color = Color.parseColor("#ffE6F60A")
        mSolidPaint.alpha=alpha
        mShadowPaint.alpha=alpha

        mFoldWidth = (width / spiltNum).toFloat()
        var mFoldItemWidth = (bitmap.width * mFactory / spiltNum).toFloat()
        sTransHeight =
            Math.sqrt((mFoldWidth * mFoldWidth - mFoldItemWidth * mFoldItemWidth).toDouble())
                .toFloat()
        for(i in 0  until  spiltNum){
            var src = floatArrayOf(
                mFoldWidth * i,
                0f,
                mFoldWidth * (i+1),
                0f,
                mFoldWidth * (i+1),
                bitmap.height.toFloat(),
                mFoldWidth * i,
                bitmap.height.toFloat()
            )
            var isDown = i%2==0
            var dst = floatArrayOf(
                mFoldItemWidth*i,
                if (isDown) 0f else sTransHeight,

                mFoldItemWidth * (i+1),
                if (isDown) sTransHeight  else 0f,

                mFoldItemWidth * (i+1),
                bitmap.height.toFloat()+ if (isDown) sTransHeight  else 0f,

                mFoldItemWidth*i,
                bitmap.height.toFloat() +  if (isDown) 0f else sTransHeight
            )
            mMatrix[i].setPolyToPoly(src, 0, dst, 0, src.size shr 1)
        }


    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for(i in 0  until  spiltNum) {

            canvas.save()
            val rect = Rect(mFoldWidth.toInt()*i, 0, mFoldWidth.toInt() * (i+1), height);
            canvas.setMatrix(mMatrix[i])
            canvas.clipRect(rect)
            canvas.drawBitmap(bitmap, (width-bitmap.width)/2f,(height-bitmap.height)/2f, null)

            //画阴影
            mLinearGradient = LinearGradient((mFoldWidth*i).toFloat(),0f,mFoldWidth*(i+1),0f,Color.BLACK,Color.TRANSPARENT,Shader.TileMode.CLAMP)
            mShadowPaint.shader=mLinearGradient

            var top = (height-bitmap.height)/2f
            if (i%2==0){
                canvas.drawRect(mFoldWidth*i,top,mFoldWidth*(i+1),top+bitmap.height.toFloat(),mSolidPaint)
            }else{
                canvas.drawRect(mFoldWidth*i,top,mFoldWidth*(i+1),top+bitmap.height.toFloat(),mShadowPaint)
            }

            canvas.restore()
        }
    }


}